﻿/*
 *  This file is part of SXAPI.
 *
 *  SXAPI is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, version 3 of the License.

 *  SXAPI is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with SXAPI.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StackExchangeApi
{
    /// <summary>
    /// An companion class to Answer that eagerly fetches
    /// all of its properties.
    /// 
    /// To use, obtain an Answer instance and cast it (implicitly
    /// or otherwise) to an EagerAnswer.
    /// </summary>
    public sealed class EagerAnswer : Post
    {
        /// <summary>
        /// Implicitly cast an Answer to an EagerAnswer.
        /// 
        /// Doing so causes all the Properties on the Answer
        /// to be fetched.
        /// </summary>
        /// <param name="answer">The Answer to eagerly load.</param>
        /// <returns>An EagerAnswer with all its properties fetched.</returns>
        public static implicit operator EagerAnswer(Answer answer)
        {
            return new EagerAnswer(answer);
        }

        /// <summary>
        /// Whether this answer was accepted or not
        /// </summary>
        public bool AcceptedAnswer
        {
            get;
            internal set;
        }

        /// <summary>
        /// The date and time this answer was created
        /// </summary>
        public DateTime Published
        {
            get;
            internal set;
        }

        /// <summary>
        /// The last time this answer was updated
        /// </summary>
        public DateTime Updated
        {
            get;
            internal set;
        }

        /// <summary>
        /// Construct a new EagerAnswer.
        /// 
        /// Uses reflection to iterate over all properties on
        /// ans and set its own properties accordingly.
        /// 
        /// This guarantees that all would-be-delayed loading
        /// is infact run immediately.
        /// </summary>
        /// <param name="ans">The Answer in question</param>
        internal EagerAnswer(Answer ans)
            : base(ans.Id, ans.Site)
        {
            Utils.EagerlyLoad(ans, this, typeof(Answer), typeof(EagerAnswer));
        }
    }
}
